package com.wonders.module.auth.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.wonders.common.constant.CommonConstant;
import com.wonders.framework.dao.impl.BaseDaoImpl;
import com.wonders.framework.entity.vo.Parameter;
import com.wonders.module.auth.constant.AuthConstant;
import com.wonders.module.auth.dao.MenuDao;
import com.wonders.module.auth.entity.bo.Menu;
import com.wonders.module.auth.entity.vo.MenuVo;

/**
 * 菜单信息数据层实现
 * 
 * @author WuBin
 * 
 */
@Repository
public class MenuDaoImpl extends BaseDaoImpl implements MenuDao {

	public List<Menu> getUserMenu(Long userId) {
		String hql = "select m from Menu m, RoleAuth ra, Role r, UserRole ur where m.id = ra.authId and r.id = ra.roleId and ur.roleId = r.id and ur.userId = :userId and m.enable = :enable and r.enable = :enable and ra.authType = :authType order by m.sort";
		List<Parameter> args = new ArrayList<Parameter>();
		args.add(new Parameter("userId", userId));
		args.add(new Parameter("enable", CommonConstant.Status.ENABLED));
		args.add(new Parameter("authType", AuthConstant.AuthType.MENU));
		return super.listByHql(hql, args, Menu.class);
	}

	public List<Menu> query(MenuVo menuVo) {
		Criteria criteria = super.getCurrentSession()
				.createCriteria(Menu.class);
		if (menuVo.getParentId() != null) {
			criteria.add(Restrictions.eq("parentId", menuVo.getParentId()));
		}
		criteria.add(Restrictions.eq("enable", menuVo.getEnable()));
		criteria.addOrder(Order.asc("sort"));
		return super.listByCriteria(criteria, Menu.class);
	}

	public void remove(Long id) {
		String hql = "update Menu m set m.enable = :enable where m.id = :id";
		List<Parameter> args = new ArrayList<Parameter>();
		args.add(new Parameter("enable", CommonConstant.Status.DISABLED));
		args.add(new Parameter("id", id));
		super.excuteHql(hql, args);
	}

}
